<?php

namespace app\official\controller;
// 微信公众号配置
use addons\wechat\library\Config;
// 微信公众号封装类
use EasyWeChat\Factory;

use think\Controller;

class Index extends Controller
{
    public $app = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->app = Factory::officialAccount(Config::load());
    }
    /**
     * 创建菜单
     */
    public function create()
    {
        $buttons = [
            [
                "type" => "view",
                "name" => "商城",
                "url"  => "http://jie.vue.jied.top"
            ],
            [
                "name"       => "菜单",
                "sub_button" => [
                    [
                        "type" => "click",
                        "name" => "点赞",
                        "key" => "点赞成功"
                    ],
                    [
                        "type" => "location_select",
                        "name" => "发送位置",
                        "key"  => "rselfmenu_2_0"
                    ],
                    [
                        "type" => "scancode_waitmsg",
                        "name" => "扫码带提示",
                        "key" => "扫码成功"
                    ],
                ],
            ],
            [
                "type" => "view",
                "name" => "H5",
                "url"  => "http://stock.jied.top"
            ],
        ];
        $result = $this->app->menu->create($buttons);
        if ($result) {
            echo '创建成功';
        } else {
            echo '创建失败';
        }
    }
    /**
     * 发送模板消息
     */
    public function send()
    {
        if ($this->request->isPost()) {
            $touser = $this->request->param('touser', '', 'trim');
            if ($touser) {
                $result = $this->app->template_message->send([
                    'touser' => $touser,
                    'template_id' => 'ZqmI0bbFo4K-4hmAPeaNd3iVAN8-1GaUdhymiYDUI9Q',
                    'url' => 'http://jie.vue.jied.top',
                    'data' => [
                        'key' => '模板消息',
                        'value' => '618活动',
                    ],
                ]);

                if ($result) {
                    $this->success("发送成功");
                } else {
                    $this->error('发送失败');
                }
            } else {
                // 获取用户列表
                $users = $this->app->user->list();

                $openidlist = $users['data']['openid'];

                foreach ($openidlist as $item) {
                    $result = $this->app->template_message->send([
                        'touser' => $item,
                        'template_id' => 'ZqmI0bbFo4K-4hmAPeaNd3iVAN8-1GaUdhymiYDUI9Q',
                        'url' => 'http://jie.vue.jied.top',
                        'data' => [
                            'key' => '模板消息',
                            'value' => '618活动',
                        ],
                    ]);

                    if (!$result) {
                        $this->error('发送失败');
                    }
                }
                $this->success("发送成功");
            }
        }

        return $this->fetch();
    }
}
